package org.opens.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.opens.interceptor.TokenInterceptor;
import org.opens.util.ContextUtils;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;

@Component
@Aspect
@Slf4j
public class TokenAop {

    @Pointcut("@annotation(org.opens.aop.aspect.Token)")
    public void serviceLimit() {

    }

    @AfterThrowing(throwing = "throwable", pointcut = "serviceLimit()")
    public void around(Throwable throwable) {
        HttpServletRequest httpRequest = ContextUtils.getHttpRequest();
        if (httpRequest != null) {
            String token = (String) httpRequest.getSession().getAttribute(TokenInterceptor.TOKEN);
            httpRequest.getSession().removeAttribute(TokenInterceptor.TOKEN);
            log.info("由异常导致的切面触发删除token: " + token);
        }
    }

}
